package org.unisinos.gc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.unisinos.gc.dao.ArtigoDAO;
import org.unisinos.gc.dao.CoautorDAO;

public class DeleteArtigoAction extends Action {
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			super.execute(mapping, form, request, response);

			String codigo = request.getParameter("codigo");
			CoautorDAO.deleteByArtigo(Integer.parseInt(codigo));
			ArtigoDAO.deleteArtigo(Integer.parseInt(codigo));
			
			return mapping.findForward("success");
		}catch (Exception e) {
			
			e.printStackTrace();
			
			ActionErrors errors = new ActionErrors();
			errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("autor.error", e.getMessage()));
			super.saveErrors(request, errors);
			
			request.setAttribute("mode", "edit");
			
			return mapping.findForward("error");
		}
		
		
		
	}

}
